package com.csthink.web.interfaces;

import com.csthink.mq.payload.RocketMessage2;
import com.csthink.mq.rocketmq.enhance.RocketMQEnhanceTemplate;
import com.csthink.web.infrastructure.mq.producer.RocketMQPublisherProperties;
import io.swagger.annotations.Api;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.UUID;

import static com.csthink.web.infrastructure.constant.AsyncMQConstants.ROCKETMQ_TAG_DELETE;

/**
 * 演示如何使用 {@link RocketMQEnhanceTemplate}
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-07
 */
@Api(tags = "Test 模块")
@RestController
@RequestMapping("enhance")
@Slf4j
public class EnhanceRocketMQTestController {

    @Setter(onMethod_ = @Autowired)
    private RocketMQEnhanceTemplate enhanceTemplate;

    @Setter(onMethod_ = @Autowired)
    private RocketMQPublisherProperties publisherProperties;

    @GetMapping("/test")
    public SendResult test() {
        String key = UUID.randomUUID().toString();

        RocketMessage2 message = new RocketMessage2();
        // 设置业务key
        message.setKey(key);
        // 设置消息来源，便于查询
        message.setSource("web");

        // 业务消息内容
        message.setUsername("jack");
        message.setBirthday(LocalDate.now());

        return enhanceTemplate.send(publisherProperties.getTopicPracticePractice2(),
                ROCKETMQ_TAG_DELETE, message);
    }
}
